package com.icss.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.icss.dao.AddressDao;
import com.icss.dao.CustomDao;
import com.icss.entity.Address;
import com.icss.entity.Admin;
import com.icss.entity.Custom;

@Controller
public class Custom_controller {
	@Autowired CustomDao cdao;
	@Autowired AddressDao adao;
	int l;
	
	@RequestMapping("hello")
//	@ResponseBody
	public String fun() {
		return "index";
	}
	@RequestMapping(value="addCustom",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String add(@RequestParam("c_name")String c_name,@RequestParam("c_pwd")String c_pwd) {
		System.out.println(c_name+"----------------------"+c_pwd);
		int r=cdao.addCustom(c_name, c_pwd);
		if(r>0) {
			return "注册成功";
		}
		else {
			return "注册失败";
		}
		
	}
	@RequestMapping("showCustom")
	public String fun10(ModelMap mp,HttpServletRequest request){
		List<Custom> clist = cdao.findCustomAll();
		mp.addAttribute("clist", clist);
		return "custom";
	}
	
	
	
	@RequestMapping(value="loginJudgement")
	@ResponseBody
	public int loginJud(@RequestParam(value="name")String c_name,@RequestParam(value="pwd")String c_pwd,HttpServletRequest request) {
		Custom custom=cdao.loginJudgement(c_name, c_pwd);
		HttpSession session = request.getSession();
		System.out.println("创建session");
		session.setAttribute("custom", custom);
		System.out.println("------------------------------------------"+custom);
		if(custom!=null) {
			
			return 1;
		}
		else {
			return 0;
		}
		
	}
	@RequestMapping(value="login_admin")
	@ResponseBody
	public int login_admin(String admin_name,String admin_pwd) {
		 Admin admin;
		 admin=cdao.login_admin(admin_name, admin_pwd);
		 if(admin!=null) {
				
				return 1;
			}
			else {
				return 0;
			}
			
	}
	
	@RequestMapping(value="delCustom",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String delCustom(Integer c_id) {
		int r=cdao.delCustom(c_id);
		if(r>0) {
			return "删除用户信息成功";
		}
		else {
			return "删除用户信息失败"; 
		}
		
	}

	@RequestMapping("loginOut")
	public String loginOut(HttpServletRequest request) {
		
			System.out.println("diaoyong");
			HttpSession session = request.getSession();
			session.removeAttribute("custom");
			System.out.println("清除custom");
			session.invalidate();
			System.out.println("清除session");
		
		return "redirect:menu/recommand.do";
	}
	@RequestMapping(value="insertAddress",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String insertAdd(String address_name,String address_sex,String address_detail,String address_phone,boolean address_default,Integer c_id) {
		System.out.println(address_name+"---"+address_sex+address_detail);
		int r=adao.insertAddress(address_phone, address_name, address_detail, address_sex, c_id, address_default);
		if(r>0) {
			return "添加地址成功";
		}
		else {
			return "添加地址失败";
		}				
	}
	@RequestMapping("showAddressByc_id")
	public String showads(ModelMap mp,String ntitle,HttpServletRequest request){
		HttpSession session=request.getSession();
		Custom custom=(Custom)session.getAttribute("custom");
		int c_id=custom.getC_id();
		List<Address> list = adao.findAddressByc_id(c_id);
		mp.addAttribute("alist", list);
		return "address";
	}
	@RequestMapping(value="updateAddress",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String updateAdd(String ads_name,String ads_sex,String ads_detail,String ads_phone,boolean ads_default,Integer ads_id) {
		System.out.println(ads_name+"---"+ads_sex+ads_detail);
		int r=adao.updateAddress(ads_phone, ads_name, ads_detail, ads_sex, ads_default,ads_id);
		if(r>0) {
			return "修改地址成功";
		}
		else {
			return "修改地址失败";
		}
	}
//	更改默认地址
	@RequestMapping("updateDefault")
	@ResponseBody
	public String updateDefault(Integer c_id,Integer address_id) {
		adao.reDefault(c_id);
		adao.updateDefault(c_id, address_id);
		return null;
		
	}
//	删除地址
	@RequestMapping(value="delAddress",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String delAddress(Integer address_id) {
		int r=adao.delAds(address_id);
		if(r>0) {
			return "删除地址成功";
		}
		else {
			return "删除地址失败"; 
		}
		
	}
	
//	修改密码
	@RequestMapping(value="repwd",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String repwd(String c_pwd,Integer c_id) {
		int r=cdao.repwd(c_pwd, c_id);
		if(r>0) {
			return "修改密码成功";
		}
		else {
			return "修改密码失败"; 
		}
		
	}
	@RequestMapping("showImg")
	@ResponseBody
	public String fun12(String c_name,String c_pwd,HttpServletResponse response) throws IOException{
		System.out.println(c_name+"-------"+c_pwd);
		Custom ct=cdao.loginJudgement(c_name, c_pwd);
		response.setContentType("image/jpg");
		ServletOutputStream out = response.getOutputStream();
		out.write(ct.getC_img());
		out.close();
		return null;
	}
	@RequestMapping(value="editNick",produces = {"application/text;charset=UTF-8"})
	@ResponseBody
	public String editNick(String c_nick,Integer c_id) {
		int r=cdao.editNick(c_nick, c_id);
		if(r>0) {
			return "设置昵称成功";
		}
		else {
			return "设置昵称失败"; 
		}
		
	}
	@RequestMapping(value="uploadImg")
	@ResponseBody
	public int fun(@RequestParam("c_img")CommonsMultipartFile c_img,Integer c_id,HttpServletRequest request) throws Exception{
		int r;
		System.out.println("up---------");
		byte[] bt=c_img.getBytes();
		r=cdao.uploadImg(bt, c_id);
		if(r>0) {
			return 1;
		}
		else {
			return 0;
		}
	}
	
	
}
